﻿Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Http


Public Class SrvSearchSrv

    Dim WithEvents ExecutionRetard As Timers.Timer
    Dim cmdArgs() As String


    Protected Overrides Sub OnStart(ByVal args() As String)
        ' Agregue el código aquí para iniciar el servicio. Este método debería poner
        ' en movimiento los elementos para que el servicio pueda funcionar.

        Try

            If args.Length Then
                ReDim cmdArgs(args.Length)
                args.CopyTo(cmdArgs, 0)
            End If

        Catch ex As Exception

        End Try

        ExecutionRetard = New Timers.Timer(10000)
        ExecutionRetard.Enabled = True

    End Sub

    Protected Overrides Sub OnStop()
        ' Agregue el código aquí para realizar cualquier anulación necesaria para detener el servicio.

        GC.Collect()
        GC.WaitForPendingFinalizers()

    End Sub

    Private Sub ExecutionRetard_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles ExecutionRetard.Elapsed

        Dim iChannel As Integer = 7080
        ' See if there are any arguments.
        If cmdArgs.Length > 0 Then
            For argNum As Integer = 0 To UBound(cmdArgs, 1)
                cmdArgs(argNum) = cmdArgs(argNum).ToUpper
                If cmdArgs(argNum).Contains("Channel:") Then
                    iChannel = Val(Mid(cmdArgs(argNum), cmdArgs(argNum).IndexOf("Channel:") + 7))
                End If
            Next argNum
        End If
        ' Insert call to appropriate starting place in your code.
        ' On return, assign appropriate value to returnValue.
        ' 0 usually means successful completion.

        Console.WriteLine("Channel " & CStr(iChannel))
        Dim channel As New HttpChannel(iChannel)
        ChannelServices.RegisterChannel(channel, False)

        Dim object1 As New TfsSearchCode.TFSSearchCodeClass()

        ' Creates the single instance of ServiceClass. All clients
        ' will use this instance.
        Dim ref1 As ObjRef = RemotingServices.Marshal(object1, "object1uri")
        Console.WriteLine("ObjRef.URI: " & ref1.URI)

        Console.WriteLine("Running. Press Enter to end publication.")
        Console.ReadLine()

        ' This unregisters the object from publication, but leaves
        ' the channel listening.
        RemotingServices.Disconnect(object1)
        Console.WriteLine()
        Console.WriteLine("Disconnected the object. Client now receives a RemotingException.")
        Console.WriteLine("Press Enter to unregister the channel.")
        Console.ReadLine()
        ' At this point, the ServerClass object still exists. The server
        ' could republish it.

        ' This unregisters the channel, but leaves the application 
        ' domain running.
        ChannelServices.UnregisterChannel(channel)
        Console.WriteLine("Unregistered the channel. Client now receives a WebException.")
        ' The ServerClass object still exists. The server could
        ' reregister the channel and republish the object.
        Console.WriteLine("The host application domain is still running. Press Enter to stop the process.")
        Console.ReadLine()


    End Sub
End Class
